***CLARIFY for HLM results******From estsimp*************************** SIMULATION UTILITIES *****************************! version 1.3.1  April 24, 1999  Michael Tomz* Simulates parameters from multivar normal after a model has been estimated* Inputs:  b, a vector containing the last parameter estimates*          v, the variance-covariance matrix of the last estimates*          sims, the number of simulations*          genname, a stub-name for variables to generate*          antisim, telling simp to perform antithetical simulations*          drawt, for drawing b's from multi-T rather than multi-Normal* Outputs: simulated parameters are saved to dataset in memory*          newvars, the names of new variables holding the simd parameters*          namepVC, the names of all parameters in the VC that were simulated* Question: does drawt work adequately in all cases?  Reads e(df_r)...program define _simp, rclass   version 6.0   di _n "Simulating main parameters.  Please wait...."   syntax [, B(string) V(string) Sims(int 1000) Genname(string) ANTIsim DRAWT]   * GENERATE RANDOM NORMAL OR RANDOM T VARIABLES   if `sims' > _N {                                 /* expand ds to fit sims*/      di in y _n "Note: Clarify is expanding your dataset from " _N /*         */ " observations to `sims'" _n "observations in order to " /*         */ "accommodate the simulations.  This will append" _n "missing " /*         */ "values to the bottom of your original dataset." _n      qui set obs `sims'   }               if "`antisim'"~="" {                             /* antithetical sims    */      local top = int(`sims'/2 + .5)                /*   calculate boundary */      local bot = `top' + 1                         /*   for top&bottom half*/   }   if "`drawt'" ~= "" {                             /* for drawing from T   */      tempvar u tfactor                             /* rather than Normal   */      qui g `u' = uniform() in 1/`sims'      if "`antisim'"~="" { qui replace `u'=1-`u'[_n-`top'] in `bot'/`sims' }      qui gen `tfactor' = sqrt(e(df_r)/invchi(e(df_r),`u')) in 1/`sims'   }   local numpVC = colsof(`v')   local i 1   while `i' <= `numpVC' {      tempvar u c`i'      qui g `u' = uniform() in 1/`sims'      if "`antisim'"~="" { qui replace `u'=1-`u'[_n-`top'] in `bot'/`sims' }      if "`drawt'" == "" { qui gen `c`i''= invnorm(`u') in 1/`sims' }      else { qui gen `c`i'' = invnorm(`u')*`tfactor' in 1/`sims' }      local cnames `cnames' `c`i''                  /* collect names of vars*/      local newvars `newvars' `genname'`i'          /* collect names newvars*/      local i = `i' + 1   }   * SIMULATE BETAS FROM NORMAL OR T DISTRIBUTION   tempname A row   *_chol `v' `numpVC'                              /*FS:_chol seemed flaky here*/   matrix `A' = cholesky(`v')						/*FS: so I just inserted cholesky()*/   matrix colnames `A' = `cnames'                /* cols to `c1'..`c`numpVC'' */   matrix colnames `A' = sameeq:                 /* Thx to Randy Stevenson */   di "% of simulations completed: " _c           local i 1   while `i' <= `numpVC' {      di int(`i'*100/`numpVC') "% " _c             /* display progress     */      matrix `row' = `A'[`i',1...]                  /* get i^th row of A    */      tempvar b`i'                                  /* temporary variable   */      matrix score `b`i'' = `row'                   /* c(NxK) * row(1xK)'   */      qui replace `b`i'' = `b`i'' + `b'[1,`i']      /* add mean             */      local i = `i' + 1   }   * SAVE AND LABEL THE PARAMETERS   local namepVC : colnames(`v')                    /* all parameters in VC */   local eqnames : coleq(`v')                       /* all equs             */   tokenize "`eqnames'"                             /* check for distinct   */   if "`1'" ~= "_" { local haseqnm 1 }              /*   equation names in  */   else { local haseqnm 0 }                         /*   the var-cov matrix */   local i 1   while `i' <= `numpVC' {                          /* for each parameter:  */      qui gen `genname'`i' = `b`i''                 /*   save sims to dset  */      local pname : word `i' of `namepVC'           /*   fetch name of param*/      * if has equation name, add eqname to label      if `haseqnm' {                                         local eqname : word `i' of `eqnames'                label var `genname'`i' "Simulated `eqname':`pname' parameter"      }                                    * otherwise use simple label w/o an eqname      else { label var `genname'`i' "Simulated `pname' parameter" }      local i = `i' + 1   }   order `newvars'   di _n   return local newvars `newvars'  /* names of newvars that were created */end***program define estsimp1, eclass   version 6.0*generate simulated coefficientsmatrix b = e(b)matrix V = e(V)_simp, b(b) v(V) s(1000) g(c) drawtlocal newvars `r(newvars)'end